﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.title
{
    public partial class news : BaseAdmin
    {
        readonly DALNews theNews = new DALNews();
        tbNews oNews;

        private void BindGrid()
        {   // 1.设置总项数
            Grid1.RecordCount = theNews.GetRecordCount(GetSqlWhere);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "newsId desc ";
            }
            DataTable table = theNews.GetListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];

            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }

        void DeleletArticle()
        {
            if (Request.QueryString["action"] == "del")
            {
                theNews.Delete(int.Parse(Request.QueryString["id"]));
                ExtAspNet.Alert.Show("删除文章成功！");
            }
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.AppendLine("1=1");

                if (this.txtKeywords.Text.Trim() != "")
                {
                    strWhere.AppendFormat(" and (newsName like '%{0}% or newsContent like '%{0}%')", this.txtKeywords.Text.Trim());
                }
                return strWhere.ToString();
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.ArticleList);
                this.DeleletArticle();
                this.BindGrid();
            }
        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theNews.GetListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

     

    }
}